package com.ykt.model;

import com.jfinal.plugin.activerecord.Page;
import com.ykt.common.constant.ParamNames;
import com.ykt.common.utils.PageUtil;
import com.ykt.common.utils.page.PageData;
import com.ykt.common.utils.page.PageParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品
 *
 * @author wing
 * @version 1.0.0
 */
public class Goods extends BaseModel<Goods> {
    private static final long serialVersionUID = 1L;

    public static final String goods_no = "goods_no";
    public static final String mg_cat_id = "mg_cat_id";
    public static final String sg_cat_id = "sg_cat_id";
    public static final String market_id = "market_id";
    public static final String shop_id = "shop_id";
    public static final String icon = "icon";
    public static final String init_price = "init_price";
    public static final String show_init_price = "show_init_price";
    public static final String sale_price = "sale_price";
    public static final String stock = "stock";
    public static final String unit_id = "unit_id";
    public static final String status = "status";
    public static final String original = "original";
    public static final String summary = "summary";

    public static final Goods dao = new Goods();

    // 这里应该使用Publish和UnPublish更合适
    public enum Status{
        IN, // 上架
        OUT,// 下架
    }

    // 商品推荐类型
    public enum RecommendType{
        /**客户端首页*/
        APP_INDEX,
        /**店铺的首页*/
        SHOP_INDEX
    }

    @Override
    public Page<Goods> paginate(int pageNumber, int rows, Map<String, String> paramMap) {
        String select = "SELECT mc.name mgCatName,c.name sgCatName, s.name shopName, CONCAT(u.`name`,IFNULL(u.summary,'')) unitName, "
        		+ "ga.type appRec,gs.type shopRec,g.*";
        String exceptSelect = " FROM goods g "
                + " LEFT JOIN shop s ON g.shop_id=s.id"
                + " LEFT JOIN goods_category c ON g.sg_cat_id=c.id "
                + " LEFT JOIN category mc ON g.mg_cat_id=mc.id "
                + " LEFT JOIN goods_unit u on g.unit_id=u.id"
                + " LEFT JOIN goods_recommend ga ON (g.id=ga.goods_id AND ga.type='APP_INDEX')"
                + " LEFT JOIN goods_recommend gs ON (g.id=gs.goods_id AND gs.type='SHOP_INDEX')" ;

        String sqlOrderBy = " ORDER BY g.id DESC ";
        paramMap.put(OB, sqlOrderBy);

        return this.paginate(select, exceptSelect, pageNumber, rows, paramMap);
    }


    public PageData<Goods> paginate(PageParam<Map<String, Object>> param) {
        String select = "SELECT g.*, gu.name unitName, gu.summary unitSummary, s.name shopName";
        String from = " FROM goods g LEFT JOIN goods_unit gu ON g.unit_id=gu.id LEFT JOIN shop s ON g.shop_id=s.id";
        String where = " WHERE 1=1";
        String sort = " ORDER BY sg_cat_id";

        List<Object> values = new ArrayList<>();

        if(param.getP().get(ParamNames.shopId) != null){
            where += " AND g.shop_id=?";
            values.add(param.getP().get(ParamNames.shopId));
        }
        if(param.getP().get(ParamNames.marketId) != null){
            where += " AND g.market_id=?";
            values.add(param.getP().get(ParamNames.marketId));
        }
        if(param.getP().get(ParamNames.categoryId) != null){
            where += " AND g.mg_cat_id=?";
            values.add(param.getP().get(ParamNames.categoryId));
        }
        if(param.getP().get(ParamNames.status) != null){
            where += " AND g.status=?";
            values.add(param.getP().get(ParamNames.status));
        }

        from += where + sort;

        Page<Goods> page = this.paginate(param.getPageNo(), param.getPageSize(), select, from, values.toArray());
        return PageUtil.to(page);
    }


}
